<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Privilege extends CI_Controller
{
    protected $class_name;
    protected $dwt_list;
    protected $dwt_info;
    protected $myModel;
    protected $datas;
    protected $result = array('error' => 0, 'message' => '', 'content' => '', 'append_data' => '');

    function __construct()
    {
        parent::__construct();
        $this->class_name           = strtolower(__CLASS__);
        $this->datas['full_page']   = true;
        $this->datas['page_title']  = "Login";
        $this->datas['my_position'] = $this->class_name;
        $this->datas['my_router']   = $this->class_name;

        $this->sess_id            = isset($_SESSION['session_id']) ? $_SESSION['session_id'] : '';
        $this->helper_id          = isset($_SESSION['user_id']) ? intval($_SESSION['user_id']) : 0;
        $this->mobile             = isset($_SESSION['mobile']) ? trim($_SESSION['mobile']) : '';//用户名就是手机号
        $this->real_name          = isset($_SESSION['real_name']) ? trim($_SESSION['real_name']) : '';
        $this->datas['sess_id']   = $this->sess_id;
        $this->datas['helper_id'] = $this->helper_id;
        $this->datas['mobile']    = $this->mobile;

        $this_model = $this->class_name . '_model';
        $this->load->model($this_model);
        $this->myModel = $this->$this_model;

    }

    public function index()
    {
        $this->signin();
    }

    /**
     * 用户登出
     * @access        public
     * @return      redirect
     */
    public function logout()
    {
        $log_info = sprintf("[%s]登出！", $this->real_name);
        $this->myModel->unset_session_cookie();
        $links = array('text' => "返回登录页面", 'href' => 'privilege/signin');
        show_message('已退出系统！', $links, 'error');
    }

    /**
     * 用户登录
     * @access    public
     * @param   string $action
     * @return  view
     */
    public function signin($action = 'form')
    {
        $get      = $this->input->get();
        $redirect = isset($get['redirect']) ? trim($get['redirect']) : '';
        if ($action == 'done') {
            $post       = $this->input->post();
            $username   = isset($post['username']) ? trim($post['username']) : '';
            $password   = isset($post['password']) ? trim($post['password']) : '';
            $autoAcount = isset($post['autoAcount']) ? intval($post['autoAcount']) : 0;
            $autoLogin  = isset($post['autoLogin']) ? intval($post['autoLogin']) : 0;

            if (empty($username) || empty($password)) {
                make_json_error("请输入正确的账号和密码！");
            }

            $autoLogin_time = $autoAcount_time = '';
            if ($autoAcount > 0) {
                $autoAcount_time = (24 * 3600) * 180;
                $this->input->set_cookie("lci_account", $username, $autoAcount_time);
            } else {
                $this->input->set_cookie("lci_account", '', '');
            }
            if ($autoLogin > 0) {
                $autoLogin_time = (24 * 3600) * 7;
                $this->myModel->set_auto_login(TRUE);
            }
            $this->input->set_cookie("lcikp_si", $autoLogin, $autoLogin_time);
            $result = $this->myModel->signin($username, $password);
            if ($result['error'] == 1) {
                make_json_error($result['message'], array('follow' => 'login-username'));
            } else {
                $append_data['followUp'] = 'redirect';
                $append_data['redirect'] = $append['redirect'] = empty($redirect) ? site_url('./') : $redirect;
                make_json_result($result, '', $append);//登录成功
            }
        } else {
            $this->datas['auto_acount'] = $this->input->cookie('lci_account', TRUE);
            $this->datas['signup_step'] = 'step1';
            $this->datas['redirect']    = rawurlencode($redirect);
            $this->load->view('signin', $this->datas);
        }
    }

}